function th5dataput()

fprintf('\tTesting H5DATAPUT...' );
tfile='testlink6.h5';
gentestfile(tfile);

test_large_add(tfile);

test_compact_add(tfile);

test_compressed_add(tfile);

test_chunked_add(tfile);

test_oversize_add(tfile);

%test_rewrite(tfile);

test_complex(tfile);

test_1d_add(tfile);
 
%test_struct(tfile);

test_cellstr(tfile);

test_ragged_array(tfile);

test_mdimcellstr(tfile);

delete(tfile);

fprintf('OK\n');
end

function gentestfile(fname)

h5id=h5filecreate(fname);
%add a group
h5groupput(h5id,'/testgroup');
%add a second group
h5groupput(h5id,'/testgroup2');
%create a subgroup
h5groupput(h5id,'/testgroup3');

H5F.close(h5id);
end

function test_large_add(fname)
md=rand(1000,1000);
res=h5dataput(fname,'/testgroup/data1',md);
if (res==-1)
    error('Error in h5dataput');
end
res=h5dataput(fname,'/testgroup/data2',single(md));
if (res==-1)
    error('Error in h5dataput');
end
res=h5dataput(fname,'/testgroup/data3',int32(md));
if (res==-1)
    error('Error in h5dataput');
end

mtest=hdf5read(fname,'/testgroup/data1');
if (~isequal(class(mtest),'double'))
    error('incorrect type');
else
    if (~isequal(mtest,md))
        error('data doesn''t match');
    end
end

mtest=hdf5read(fname,'/testgroup/data2');
if (~isequal(class(mtest),'single'))
    error('incorrect type');
else
    if (~isequal(mtest,single(md)))
        error('data doesn''t match');
    end
end

mtest=hdf5read(fname,'/testgroup/data3');
if (~isequal(class(mtest),'int32'))
    error('incorrect type');
else
    if (~isequal(mtest,int32(md)))
        error('data doesn''t match');
    end
end
end

function test_compact_add(fname)

md=rand(10,100);
res=h5dataput(fname,'/testgroup2/data1',md,'compact',1);
if (res==-1)
    error('Error in h5dataput');
end
mtest=hdf5read(fname,'/testgroup2/data1');
if (mtest~=md)
    error('data not matching');
end
mdi=hdf5info(fname);
if ~isequal(mdi.GroupHierarchy.Groups(2).Datasets.Layout,'compact')
error('layout not specified compact');
end


end

function test_1d_add(fname)

md=round(rand(1000,1));
md=int32(md);
%mainly testing error faults not identical results
res=h5dataput(fname,'/testgroup2/data_1d',md,'compression',3);
res=res+h5dataput(fname,'/testgroup2/data_1d2',md);
res=res+h5dataput(fname,'/testgroup2/data_1d3',md,'max_size',10000);
if (res<0)
    error('Error in h5dataput');
end
mtest=hdf5read(fname,'/testgroup2/data_1d');
if (mtest~=md)
    error('data not matching');
end

mtest=hdf5read(fname,'/testgroup2/data_1d2');
if (mtest~=md)
    error('data not matching');
end
mtest=hdf5read(fname,'/testgroup2/data_1d3');
if (mtest~=md)
    error('data not matching');
end
end

function test_compressed_add(fname)
d_old=dir(fname);
md=round(rand(10000,100)*62);
md=int32(md);
res=h5dataput(fname,'/testgroup2/data2',md,'compression',3);
if (res==-1)
    error('Error in h5dataput');
end
d_new=dir(fname);

dsize=d_new.bytes-d_old.bytes;
if (dsize>=10000*100*4)
    error('compression failed');
end
mtest=hdf5read(fname,'/testgroup2/data2');
if (mtest~=md)
    error('data not matching');
end
end

function test_chunked_add(fname)
md=rand(100,100);
res=h5dataput(fname,'/testgroup2/data3',md,'chunk_size',[10,10]);
if (res==-1)
    error('Error in h5dataput');
end
mtest=hdf5read(fname,'/testgroup2/data3');
if (mtest~=md)
    error('data not matching');
end
mdi=hdf5info(fname);
if ~isequal(mdi.GroupHierarchy.Groups(2).Datasets(3).Chunksize,[10,10])
    error('chunk size not recorded correctly');
end
end

function test_oversize_add(fname)
md=rand(100,100);
res=h5dataput(fname,'/testgroup3/data1',md,'max_size',[1000,100]);
if (res==-1)
    error('Error in h5dataput');
end
mtest=hdf5read(fname,'/testgroup3/data1');
if (mtest~=md)
    error('data not matching');
end
mdi=hdf5info(fname);
if ~isequal(mdi.GroupHierarchy.Groups(3).Datasets.MaxDims,[1000,100])
    error('max size not recorded correctly');
end
if ~isequal(mdi.GroupHierarchy.Groups(3).Datasets.Dims,[100,100])
    error('size not recorded correctly');
end
end

function test_rewrite(fname)
%test rewriting an existing data set that doesn't fit the new data
md=rand(1000,1000);
res=h5dataput(fname,'/testgroup2/data3',md);
if (res==-1)
    error('Error in h5dataput');
end
mtest=hdf5read(fname,'/testgroup2/data3');
if (mtest~=md)
    error('data not matching');
end
end

function test_complex(fname)
md=rand(10,10)+1j*rand(10,10);
res=h5dataput(fname,'/testgroup3/data2',md);
if (res==-1)
    error('Error in h5dataput');
end
mtest=h5varget(fname,'/testgroup3/data2');
if (mtest~=md)
    error('data not matching');
end
end

function test_struct(fname)
md.a=rand(100,1);
md.b=rand(100,1);
md.c=rand(100,1);

res=h5dataput(fname,'/testgroup2/data4',md);
if (res==-1)
    error('Error in h5dataput');
end
mtest=h5varget(fname,'/testgroup2/data4');
if (~isequal(mtest,md))
    error('data not matching');
end

end

function test_cellstr(fname)

str1={'apple','banana','carrot','daikon','eggplant','fruit','granola'};

res=h5dataput(fname,'/testgroup2/strdata',str1);
if (res==-1)
    error('Error in h5dataput');
end
mtest=h5varget(fname,'/testgroup2/strdata');
if (~isequal(mtest,str1))
    error('data not matching');
end

end


function test_mdimcellstr(fname)

str1={'apple','banana','carrot';'daikon','eggplant','fruit';'granola','herb','iceberg lettuce'};

res=h5dataput(fname,'/testgroup2/strdata2',str1);
if (res==-1)
    error('Error in h5dataput');
end
mtest=h5varget(fname,'/testgroup2/strdata2');
if (~isequal(mtest,str1))
    error('data not matching');
end

end

function test_ragged_array(fname)

r1={[45,30],[200,45,345],23,[1,3,4,5,6]};

res=h5dataput(fname,'/testgroup2/raggeddata',r1);
if (res==-1)
    error('Error in h5dataput');
end
mtest=h5varget(fname,'/testgroup2/raggeddata');
if (~isequal(mtest,r1))
    error('data not matching');
end

end